<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
    >
                  <el-form-item label="用户编号" prop="userId">
                    <el-input
                        v-model="queryParams.userId"
                        placeholder="请输入用户编号"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="联系人手机号" prop="contactUserMobile">
                    <el-input
                        v-model="queryParams.contactUserMobile"
                        placeholder="请输入联系人手机号"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="联系人姓名" prop="contactUserRealName">
                    <el-input
                        v-model="queryParams.contactUserRealName"
                        placeholder="请输入联系人姓名"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="区域编码" prop="areaCode">
                    <el-input
                        v-model="queryParams.areaCode"
                        placeholder="请输入区域编码"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="问题类型" prop="qaType">
                      <el-select
                          v-model="queryParams.qaType"
                          placeholder="请选择问题类型"
                          clearable
                          class="!w-240px"
                      >
                        <el-option label="请选择字典生成" value=""/>
                  </el-select>
                  </el-form-item>
                  <el-form-item label="具体事项" prop="qaDetailType">
                      <el-select
                          v-model="queryParams.qaDetailType"
                          placeholder="请选择具体事项"
                          clearable
                          class="!w-240px"
                      >
                        <el-option label="请选择字典生成" value=""/>
                  </el-select>
                  </el-form-item>
                  <el-form-item label="提问方式" prop="qaMode">
                    <el-input
                        v-model="queryParams.qaMode"
                        placeholder="请输入提问方式"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="首次内容缩写" prop="qaBgContentOmit">
                    <el-input
                        v-model="queryParams.qaBgContentOmit"
                        placeholder="请输入首次内容缩写"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="用户昵称" prop="nickName">
                    <el-input
                        v-model="queryParams.nickName"
                        placeholder="请输入用户昵称"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="用户手机号" prop="mobile">
                    <el-input
                        v-model="queryParams.mobile"
                        placeholder="请输入用户手机号"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="问题标题" prop="qaTypeTitle">
                    <el-input
                        v-model="queryParams.qaTypeTitle"
                        placeholder="请输入问题标题"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="具体事项" prop="qaDetailTypeTitle">
                    <el-input
                        v-model="queryParams.qaDetailTypeTitle"
                        placeholder="请输入具体事项"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="省份" prop="provinceName">
                    <el-input
                        v-model="queryParams.provinceName"
                        placeholder="请输入省份"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                  <el-form-item label="市区" prop="areaName">
                    <el-input
                        v-model="queryParams.areaName"
                        placeholder="请输入市区"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
                      <el-form-item label="创建时间" prop="createTime">
                        <el-date-picker
                            v-model="queryParams.createTime"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            type="daterange"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
                            class="!w-240px"
                        />
                      </el-form-item>
                  <el-form-item label="关联线索ID" prop="adClueId">
                    <el-input
                        v-model="queryParams.adClueId"
                        placeholder="请输入关联线索ID"
                        clearable
                        @keyup.enter="handleQuery"
                        class="!w-240px"
                    />
                  </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px"/>
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px"/>
          重置
        </el-button>
          <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['ad:legal-advice-clue-qa:create']"
          >
        <Icon icon="ep:plus" class="mr-5px"/>
        新增
      </el-button>
        <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['ad:legal-advice-clue-qa:export']"
        >
          <Icon icon="ep:download" class="mr-5px"/>
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
                  <el-table-column label="对话记录编号" align="center" prop="id" width="150px"/>
                  <el-table-column label="用户编号" align="center" prop="userId" width="150px"/>
                  <el-table-column label="联系人手机号" align="center" prop="contactUserMobile" width="150px"/>
                  <el-table-column label="联系人姓名" align="center" prop="contactUserRealName" width="150px"/>
                  <el-table-column label="区域编码" align="center" prop="areaCode" width="150px"/>
                  <el-table-column label="问题类型" align="center" prop="qaType" width="150px"/>
                  <el-table-column label="具体事项" align="center" prop="qaDetailType" width="150px"/>
                  <el-table-column label="提问方式" align="center" prop="qaMode" width="150px"/>
                  <el-table-column label="首次内容缩写" align="center" prop="qaBgContentOmit" width="150px"/>
                  <el-table-column label="用户昵称" align="center" prop="nickName" width="150px"/>
                  <el-table-column label="用户手机号" align="center" prop="mobile" width="150px"/>
                  <el-table-column label="问题标题" align="center" prop="qaTypeTitle" width="150px"/>
                  <el-table-column label="具体事项" align="center" prop="qaDetailTypeTitle" width="150px"/>
                  <el-table-column label="省份" align="center" prop="provinceName" width="150px"/>
                  <el-table-column label="市区" align="center" prop="areaName" width="150px"/>
                  <el-table-column label="对话内容" align="center" prop="qaContent" width="150px"/>
                  <el-table-column
                      label="创建时间"
                      align="center"
                      prop="createTime"
                  />
                  <el-table-column label="关联线索ID" align="center" prop="adClueId" width="150px"/>
      <el-table-column label="操作" align="center" width="150px">
        <template #default="scope">
          <el-button
              link
              type="primary"
              @click="openForm('update', scope.row.id)"
              v-hasPermi="['ad:legal-advice-clue-qa:update']"
          >
            编辑
          </el-button>
          <el-button
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
              v-hasPermi="['ad:legal-advice-clue-qa:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <LegalAdviceClueQaForm ref="formRef" @success="getList"/>
</template>

<script setup lang="ts">
  import download from '@/utils/download'
  import * as LegalAdviceClueQaApi from '@/api/ad/legaladviceclueqa'
  import LegalAdviceClueQaForm from './LegalAdviceClueQaForm.vue'


  defineOptions({name: 'AdLegalAdviceClueQa'})

  const message = useMessage() // 消息弹窗
  const {t} = useI18n() // 国际化

  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const list = ref([]) // 列表的数据
  const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
                      userId: null,
                      contactUserMobile: null,
                      contactUserRealName: null,
                      areaCode: null,
                      qaType: null,
                      qaDetailType: null,
                      qaMode: null,
                      qaBgContentOmit: null,
                      nickName: null,
                      mobile: null,
                      qaTypeTitle: null,
                      qaDetailTypeTitle: null,
                      provinceName: null,
                      areaName: null,
                      qaContent: null,
                      createTime: [],
                      adClueId: null
  })
  const queryFormRef = ref() // 搜索的表单
  const exportLoading = ref(false) // 导出的加载中

  /** 查询列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await LegalAdviceClueQaApi.getLegalAdviceClueQaPage(queryParams)
      list.value = data.list
      total.value = data.total
    } finally {
      loading.value = false
    }
  }

  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
  }

  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }

  /** 添加/修改操作 */
  const formRef = ref()
  const openForm = (type: string, id?: number) => {
    formRef.value.open(type, id)
  }

  /** 删除按钮操作 */
  const handleDelete = async (id: number) => {
    try {
      // 删除的二次确认
      await message.delConfirm()
      // 发起删除
      await LegalAdviceClueQaApi.deleteLegalAdviceClueQa(id)
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
    } catch {
    }
  }

  /** 导出按钮操作 */
  const handleExport = async () => {
    try {
      // 导出的二次确认
      await message.exportConfirm()
      // 发起导出
      exportLoading.value = true
      const data = await LegalAdviceClueQaApi.exportLegalAdviceClueQa(queryParams)
      download.excel(data, '法律咨询线索对话.xls')
    } catch {
    } finally {
      exportLoading.value = false
    }
  }

  /** 初始化 **/
  onMounted(() => {
    getList()
  })
</script>